package com.huha.tools;

import com.huha.tools.utils.CreateEpointBZBIndex;
import com.huha.tools.utils.CreateVOCode;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.VBox;
import javafx.stage.Popup;

import java.net.URL;
import java.util.ResourceBundle;

import static com.huha.tools.Constants.*;

public class HUHAToolsController implements Initializable {
    @FXML
    TextArea txtinput;

    @FXML
    TextArea txtresult;

    @FXML
    ChoiceBox choDatabase;

    @FXML
    Label lblremark;

    @FXML
    Label lblvoremark;

    @FXML
    Button btncreate;

    @FXML
    TextArea txtvoresult;

    @FXML
    TextArea txtvoinput;

    @FXML
    Tab tabindex;

    @FXML
    Tab tabvo;

    // 程序初次加载时会执行的方法
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        initChoDababase();
        initRemark(lblremark, "/remark.png", 800, 300, INFO_TEXT_REMARK_CREATEINDEX);
        initRemark(lblvoremark, "/voremark.png", 200, 100, INFO_TEXT_REMARK_CREATEVO);
        addTabSelectionListener(tabindex);
        addTabSelectionListener(tabvo);
    }

    @FXML
    void btnCreateClick(ActionEvent event) {
        String inputText = txtinput.getText();
        String[] lines = inputText.split("\n");
        StringBuilder outputText = new StringBuilder();
        for (String line : lines) {
            outputText.append(CreateEpointBZBIndex.createIndex(line, choDatabase.getValue().toString()));
        }
        txtresult.setText(outputText.toString());
    }

    @FXML
    void btnCreateVOClick() {
        if ("".equals(txtvoinput.getText())) {
            txtvoresult.setText(ERROR_TEXT_INVALID_INPUT);
        } else {
            String inputText = txtvoinput.getText();
            String[] lines = inputText.split("\n");
            StringBuilder outputText = new StringBuilder();
            outputText.append(CreateVOCode.createvo(inputText));
            for (String line : lines) {
                outputText.append(CreateVOCode.createvobykey(line));
            }
            txtvoresult.setText(outputText.toString());
        }
    }

    // 初始化数据库类型下拉框
    void initChoDababase() {
        choDatabase.setItems(FXCollections.observableArrayList("MySQL", "SQLServer"));
        choDatabase.setValue("MySQL");
    }

    // 初始化使用说明
    void initRemark(Label control, String imgurl, double imgwidth, double imgheight, String remarktext) {
        VBox popupContent = new VBox(10);
        if (!"".equals(imgurl) && !"".equals(remarktext)) {
            Image image = new Image(imgurl);
            ImageView imageView = new ImageView(image);
            if (imgwidth != 0) {
                imageView.setFitWidth(imgwidth);
            } else {
                imageView.setFitWidth(200);
            }
            if (imgheight != 0) {
                imageView.setFitHeight(imgheight);
            } else {
                imageView.setFitHeight(200);
            }


            Label label = new Label(remarktext);
            label.setStyle("-fx-text-fill: red;");
            label.setWrapText(true);
            label.setMaxWidth(600);

            popupContent.setAlignment(Pos.CENTER);
            popupContent.setPadding(new Insets(10));
            popupContent.getChildren().addAll(label, imageView);
        } else if (!"".equals(remarktext)) {
            Label label = new Label(remarktext);
            label.setStyle("-fx-text-fill: red;");
            label.setWrapText(true);
            label.setMaxWidth(600);

            popupContent.setAlignment(Pos.CENTER);
            popupContent.setPadding(new Insets(10));
            popupContent.getChildren().addAll(label);
        }

        Popup popup = new Popup();
        popup.getContent().add(popupContent);

        control.setOnMouseEntered(event -> {
            if (!popup.isShowing()) {
                popup.show(control, event.getScreenX(), event.getScreenY());
            }
        });

        control.setOnMouseExited(event -> {
            if (popup.isShowing()) {
                popup.hide();
            }
        });
    }

    // 为tab添加监听，实现点击时改变tab的标题颜色
    private void addTabSelectionListener(Tab tab) {
        tab.selectedProperty().addListener(new ChangeListener<Boolean>() {
            @Override
            public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
                if (newValue) {
                    // 当 Tab 被选中时，将其标题颜色更改为绿色
                    tab.setStyle("-fx-text-fill: green;-fx-font-weight: bold");
                } else {
                    // 当 Tab 未被选中时，将其标题颜色更改为默认颜色
                    tab.setStyle("-fx-font-weight: bold");
                }
            }
        });
    }
}

